/*
 * The MIT License
 *
 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jvnet.hudson.test;

import hudson.cli.CLICommand;
import junit.framework.TestSuite;

import java.io.File;
import java.util.Map;

/**
 * Called by the code generated by maven-hpi-plugin to build tests for plugins.
 *
 * @author Kohsuke Kawaguchi
 */
public class PluginAutomaticTestBuilder {
    /**
     * @param params
     *      Various information about the plugin that maven-hpi-plugin adds.
     *      As of 1.52, this includes the followings:
     *
     *      basedir (String) : directory that contains pom.xml
     *      artifactId (String) : artifact ID of the plugin
     *      outputDirectory (String) : target/classes dir where class files and resources can be found
     *      testOutputDirectory (String) : target/test-classes.
     */
    public static TestSuite build(Map<String,?> params) throws Exception {
        TestSuite suite = new TestSuite();

        // automatic Jelly tests
        if (params.containsKey("outputDirectory") // shouldn't happen, but be defensive
        ||  notSkipTests("JellyTest")) {
            File outputDirectory = new File((String)params.get("outputDirectory"));
            suite.addTest(JellyTestSuiteBuilder.build(outputDirectory,toBoolean(params.get("requirePI"))));
        }

        suite.addTestSuite(CliSanityTest.class);

        return suite;
    }

    private static boolean toBoolean(Object requirePI) {
        if (requirePI==null)    return false;
        if (requirePI instanceof Boolean)   return (Boolean)requirePI;
        return Boolean.parseBoolean(requirePI.toString());
    }

    /**
     * Provides an escape hatch for plugin developers to skip auto-generated tests.
     */
    private static boolean notSkipTests(String propertyName) {
        return !Boolean.getBoolean("hpiTest.skip"+propertyName);
    }

    public static class CliSanityTest extends HudsonTestCase {
        public void testCliSanity() {
            CLICommand.clone("help");
        }
    }
}
